// pages/home/


const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
// const cailanzi64 = require("../../images/cailanzi64")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiper_list: [
      {src: "/images/home-img/02.jpg"},
      {src: "/images/home-img/02.jpg"},
    ],
    swiper_list_bottom: [
      {src: "/images/home-img/11.jpg"},
      {src: "/images/home-img/11.jpg"},
      // {src: "/images/home-img/12.jpg"},
    ],
    category_list: [],
    uid: 9,
    page: 1,
    page_size: 15,
    good_list: [],
    current_index: 0,
    if_follow: false,
    no_more_data: false,
    test_list: [{},{},{},{},{},{},],
    middle_list: '',
    more_list: [],
    shop_list: [],
    three_circle_list: [{
      title: '乡',
      text: "乡村振兴",
      img: '/images/home-img/0.xiang.png'
    },{
      title: '食',
      text: "食疗养生",
      img: '/images/home-img/0.shi.png'
    },{
      title: '礼',
      text: "礼品特采",
      img: '/images/home-img/0.li.png'
    },],
    brand_list: [{
      title: '品牌馆',
      img: '/images/home-img/品牌馆.png'
    },{
      title: '产地直供',
      img: '/images/home-img/产地直供.png'
    },{
      title: '帮扶馆',
      img: '/images/home-img/帮扶馆.png'
    },{
      title: '数字农业',
      img: '/images/home-img/数字农业.png'
    },],
    recommend_shop_list: [],
    brand_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init_data()
    this.get_category_list()
    this.get_goods_list()
    this.get_middle_list()
    this.get_more_list() 
    this.get_shop_list() 
    this.get_recommend_shop_list()
    this.get_brand_list()
  },
  init_data() {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
    })
  },
  get_recommend_shop_list() {
    let params = {
      page: 1,
      pageSize: 2,
      uid: this.data.uid 
    }
    request.post(api.shoplist, params).then(res=>{
      this.setData({
        recommend_shop_list: res
      })
    })
  },
  get_brand_list() {
    let params = {
      page: 1,
      pageSize: 4,
      uid: this.data.uid 
    }
    request.post(api.goods_more, params).then(res=>{
      this.setData({
        brand_list: res
      })
    })
  },

  go_zone(e) {
    let url = "/pages/home/zone?index=" + e.currentTarget.dataset.index 
    wx.navigateTo({
      url,
    })
  },
  collecting(e) {
    let is_follow = e.currentTarget.dataset.is_follow
    let gid = e.currentTarget.dataset.id
    let if_register = App.globalData.if_register
    if (!if_register) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    else {
      let params = {
        uid: this.data.uid,
        gid
      }
      if (is_follow==0) {
        this.follow_add(params)
      }
      else {
        this.follow_delete(params)
      }
    }
  },
  follow_add(params) {
    request.post(api.followadd, params).then(res=>{
      util.showToast('已成功收藏')
      this.get_goods_list()
    })
  },
  follow_delete(params) {
    request.post(api.followdelete, params).then(res=>{
      util.showToast('已取消收藏')
      console.log(this);
      this.get_goods_list()
    })
  },
  go_search() {
    wx.navigateTo({
      url: '/pages/good/search',
    })
  },
  get_new_goods_list() {
    let params = {
      page: this.data.page,
      pageSize: this.data.page_size,
      uid: this.data.uid 
    }
    let old_list = this.data.good_list
    request.post(api.goods_more, params).then(res=>{
      if (res.length != 0) {
        let new_list = old_list.concat(res)
        this.setData({
          good_list: new_list
        }) 
      }
      else {
        this.setData({
          noMoreData: true
        });
        util.showToast('暂无更多商品了');
      }
    })
  },
  get_goods_list() {
    let params = {
      page: this.data.page,
      pageSize: this.data.page_size,
      uid: this.data.uid 
    }
    request.post(api.goods_more, params).then(res=>{
      this.setData({
        good_list: res
      })
    })
  },
  get_shop_list() {
    let params = {
      page: this.data.page,
      pageSize: 9,
      uid: this.data.uid 
    }
    request.post(api.shoplist, params).then(res=>{
      this.setData({
        shop_list: res
      })
    })
  },
  get_middle_list() {
    let params = {
      page: 1,
      pageSize: 6,
      uid: this.data.uid 
    }
    request.post(api.goods_more, params).then(res=>{
      this.setData({
        middle_list: res
      })
    })
  },
  get_category_list() {
    let params = {
      pid: 0,
      page: this.data.page,
      pageSize: 3,
    }
    request.post(api.category_list, params).then(res=> {
      if (res.length != 0) {
        this.setData({
          category_list: res
        })
      }
    })
  },
  go_welfare() {
    let url = '/pages/welfare/welfare'
    wx.navigateTo({
      url,
    })
  },
  go_brand() {
    let url = '/pages/brand/brand'
    wx.navigateTo({
      url,
    })
  },
  go_vip() {
    let url = '/pages/vip/vip'
    wx.navigateTo({
      url,
    })
  },
  indicator_change(event) {
    this.setData({
      current_index: event.detail.current
    })
  },
  category_navigation(e) {
    let index = e.currentTarget.dataset.index
    let url = '/pages/home/classify?index=' + index
    wx.reLaunch({
      url,
    })
  },
  get_more_list() {
    let params = {
      key: this.data.keyword_value,
      uid: this.data.uid,
    }
    util.showLoading('加载中')
    request.post(api.index, params).then(res=>{
      util.hideLoading()
      this.setData({
        more_list: res
      })
    })
  },
  go_shop() {
    let url='/pages/shop/shop'
    wx.navigateTo({
      url
    })
  },
  go_group_buying() {
    let url='/pages/group_buying/group_buying'
    wx.navigateTo({
      url
    })
  },
  go_shop_detail(e) {
    let id = e.currentTarget.dataset.id
    if (!id) {
      id = 32
    }
    wx.navigateTo({
      url: '/pages/shop/detail?id=' + id,
    })
  },
  go_hot_around() {
    wx.navigateTo({
      url: '/pages/hot_around/hot_around',
    })
  },
  
  go_settling() {
    wx.navigateTo({
      url: '/pages/shop/settling',
    })
  },
  go_classify() {
    wx.navigateTo({
      url: '/pages/home/baopin',
    })
  },
  go_point() {
    wx.navigateTo({
      url: '/pages/point/point',
    })
  },
  go_detail(e) {
    let id = e.currentTarget.dataset.id
    if (!id) {
      id = 32
    }
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  go_detail_from_component(e) {
    let id = e.detail.currentTarget.dataset.id
    if (!id) {
      id = 10
    }
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  go_detail_to_hot_good_around(e) {
    let id = e.detail.currentTarget.dataset.id
    if (!id) {
      id = 10
    }
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id+"&entrance=hot_good_around",
    })
  },
  cart_add(){
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res=>{
      this.setData({
        if_show_box:false
      })
      util.showToast("已加入购物车")
    })
  },
  // 菜篮子跳转小程序
  go_cailanzi_program() {
    wx.navigateToMiniProgram({
      appId: 'wxb1f5b7f152b1858d',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onReachBottom: function () {
    var noMoreData = this.data.noMoreData;
    if (!noMoreData) {
      var pageno = this.data.page;
      pageno++;
      this.setData({
        page: pageno
      });
      this.get_new_goods_list();
    }
  },
})